﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

using FLEN_Sim.containerobjects;
using FLEN_Sim.config;

namespace FLEN_Sim.funct
{
    public static class DataHandler
    {
        public static void createObjectsFromXML(out List<Hospital> hospitals, out List<AmbulanceStation> ambulanceStations, out List<AmbulanceVehicle> vehicles, ref GMap.NET.WindowsForms.GMapControl control)
        {
            hospitals = new List<Hospital>();
            ambulanceStations = new List<AmbulanceStation>();
            vehicles = new List<AmbulanceVehicle>();

            using (XmlTextReader reader = new XmlTextReader(File.Open(Config.HOSPITALS, FileMode.Open)))
            {
                while (reader.Read())
                {
                    if(reader.NodeType.Equals(XmlNodeType.Element) && reader.Name.Equals("Hospital") && reader.HasAttributes)
                    {
                        String id, shortid, adress;
                        double lat, lon;

                        reader.MoveToFirstAttribute();
                        id = reader.Value;

                        reader.MoveToNextAttribute();
                        shortid = reader.Value;

                        reader.MoveToNextAttribute();
                        adress = reader.Value;

                        reader.MoveToNextAttribute();
                        lat = Double.Parse(reader.Value.Replace(".", ","));

                        reader.MoveToNextAttribute();
                        lon = Double.Parse(reader.Value.Replace(".", ","));

                        hospitals.Add(new Hospital(id, shortid, adress, lat, lon));
                    }
                }
            }

            using (XmlTextReader reader = new XmlTextReader(File.Open(Config.AMBULANCE_STATIONS, FileMode.Open)))
            {
                while (reader.Read())
                {
                    if (reader.NodeType.Equals(XmlNodeType.Element) && reader.Name.Equals("Station") && reader.HasAttributes)
                    {
                        String id, shortid, adress;
                        double lat, lon;

                        reader.MoveToFirstAttribute();
                        id = reader.Value;

                        reader.MoveToNextAttribute();
                        shortid = reader.Value;

                        reader.MoveToNextAttribute();
                        adress = reader.Value;

                        reader.MoveToNextAttribute();
                        lat = Double.Parse(reader.Value.Replace(".", ","));

                        reader.MoveToNextAttribute();
                        lon = Double.Parse(reader.Value.Replace(".", ","));

                        ambulanceStations.Add(new AmbulanceStation(id, shortid, adress, lat, lon, ref control));
                    }
                }
            }

            using (XmlTextReader reader = new XmlTextReader(File.Open(Config.VEHICLES, FileMode.Open)))
            {
                while (reader.Read())
                {
                    if (reader.NodeType.Equals(XmlNodeType.Element) && reader.Name.Equals("Vehicle") && reader.HasAttributes)
                    {
                        String callsign, stationName, crewChange, type, id;
                        String[] tiles;
                        AmbulanceStation station = null;

                        reader.MoveToFirstAttribute();
                        callsign = reader.Value;

                        reader.MoveToNextAttribute();
                        stationName = reader.Value;

                        reader.MoveToNextAttribute();
                        crewChange = reader.Value;

                        foreach (AmbulanceStation s in ambulanceStations)
                        {
                            if (s.ShortID.Equals(stationName))
                                station = s;
                        }

                        tiles = callsign.Split('-');
                        type = tiles[1];
                        id = tiles[2];

                        vehicles.Add(new AmbulanceVehicle(station, Int32.Parse(type), Int32.Parse(id), crewChange));                        
                    }
                }
            }
        }

        public static string getVehicleTypeFromTypeNumber(int TypeNumber)
        {
            switch (TypeNumber)
            {
                case 81:
                    return "NAW";
                case 82:
                    return "NEF";
                case 83:
                    return "RTW";
                case 84:
                    return "RTH";
                case 85:
                    return "KTW";
                default:
                    return null;
            }
        }
    }
}
